package com.eascs.crm.nsEditDetail.controller;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.entity.ManagementTdInfoEntity;
import com.eascs.crm.nsEditDetail.service.ManagementTdInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

import static com.eascs.site.view.HtmlRestResult.popHtmlFragment;

/**
 * 内审信息编辑
 * @author yy
 *
 */
@Controller
@RequestMapping("/management/td/rest")
public class ManagementTdInfolRestController extends BaseRestController {
	private static Logger logger = LoggerFactory.getLogger(ManagementTdInfolRestController.class);

	@Autowired
	ManagementTdInfoService tdInfoService;

	@RequestMapping("/addPage")
	public HtmlRestResult managementTdInfoAddPage(String loanapplyNo) {
		ManagementTdInfoEntity info = new ManagementTdInfoEntity();
		info.setLoanapplyNo(loanapplyNo);
		ViewContext.put("view", info);
		return popHtmlFragment("/nsEditDetail/management_td_info_add");
	}

	@RequestMapping("/add")
	public BaseRestResult<String> managementTdInfoAdd(ManagementTdInfoEntity info) {
		createManagementTdInfo(info);
		boolean flag = tdInfoService.insert(info);
		if(flag) {
			return BaseRestResult.info("保存年度经营情况成功").andRefresh();
		} else {
			return BaseRestResult.warn("保存年度经营情况失败").andRefresh();
		}
	}

	@RequestMapping("/updatePage")
	public HtmlRestResult managementTdInfoUpdatePage(String id) {
		ManagementTdInfoEntity info = tdInfoService.queryById(id);
		ViewContext.put("view", info);
		return popHtmlFragment("/nsEditDetail/management_td_info_update");
	}

	@RequestMapping("/update")
	public BaseRestResult<String> managementTdInfoUpdate(ManagementTdInfoEntity info) {
		if(StringUtil.isBlank(info.getId())){
			return BaseRestResult.warn("更新年度经营情况失败:记录不存在").andRefresh();
		}

		modifyManagementTdInfo(info);
		boolean flag = tdInfoService.update(info);
		if(flag) {
			return BaseRestResult.info("更新年度经营情况成功").andRefresh();
		} else {
			return BaseRestResult.warn("更新年度经营情况失败").andRefresh();
		}
	}

	@RequestMapping("/del")
	public BaseRestResult<String> managementTdInfoDel(String id) {
		boolean flag = tdInfoService.delete(id);
		if(flag) {
			return BaseRestResult.info("删除年度经营情况成功").andRefresh();
		} else {
			return BaseRestResult.warn("删除年度经营情况失败").andRefresh();
		}
	}

	private void createManagementTdInfo(ManagementTdInfoEntity info){
		User user;
		user = UserHolder.getUser();
		info.setCreateDate(new Date());
		info.setModifyDate(new Date());

		info.setCreateId(user.getNo());
		info.setModifyId(user.getNo());

		info.setCreator(user.getCname());
		info.setModifier(user.getCname());
	}

	private void modifyManagementTdInfo(ManagementTdInfoEntity info){
		User user = UserHolder.getUser();
		info.setModifyDate(new Date());
		info.setModifyId(user.getNo());
		info.setModifier(user.getCname());
	}
}
